\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{chimie}[02/03/2014]

\RequirePackage[version=3,arrows=pgf-filled]{mhchem}
\usetikzlibrary{decorations.markings}
\RequirePackage{chemfig}
\RequirePackage{textcomp} % \degres look better
\RequirePackage{tikz}
\usetikzlibrary{shadings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% chemistry
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% global & meta macro

\@ifundefined{dd}{\newcommand{\dd}{\ensuremath{\text{d}}}}{}
\@ifundefined{unit}{\newcommand{\unit}[1]{\ensuremath{\mathtt{#1}}}}{}
\newcommand{\optional@chem}[2][\relax]{%
\ifx#1\relax%
  \ensuremath{#2}%
\else%
  \ensuremath{#2({\scriptstyle\ce{#1}})}
\fi}
\@ifundefined{optional@sub}{%
\newcommand{\optional@sub}[2][\relax]{%
\ifx#1\relax%
  \ensuremath{#2}%
\else%
  \ensuremath{#2_{#1}}%
\fi}
}

\newcommand{\make@quantity}[3]{%
\@ifundefined{#1}{\expandafter\gdef\csname#1\endcsname{\ensuremath{#3{#2}}}}{}
}
\newcommand{\make@globalVariable}[2]{\make@quantity{#1}{#2}{}}
\newcommand{\make@globalConstant}[2]{\make@quantity{#1}{#2}{\mathrm}}

\newcommand{\make@equation}[6][0] %% name, symbol, value, dvalue, unit | symbol = value (dvalue) \unit{unit}, optional argument is power
{%
\expandafter\gdef\csname#2\endcsname{\ensuremath{#3}}%                                                          \"name"     produces "symbol"
\expandafter\gdef\csname#2val\endcsname{\numprint{#4}\ifnum#1=0\else\,10^{#1}\fi}%                              \"name"val  produces "value"
\expandafter\gdef\csname#2dval\endcsname{\numprint{#5}}%                                                        \"name"dval produces "dvalue"
\expandafter\gdef\csname#2num\endcsname{\numprint{#4}\,(\csname#2dval\endcsname)\ifnum#1=0\else\,10^{#1}\fi}%   \"name"num  produces "value (dvalue)"
\expandafter\gdef\csname#2unit\endcsname{\unit{#6}}%                                                            \"name"unit produces "unit"
\expandafter\gdef\csname#2Equation\endcsname{% %% equation
 \ensuremath{\csname#2\endcsname = \csname#2num\endcsname~\csname#2unit\endcsname}}%                            \"name"Equation produces "symbol = value (dvalue)~unit"
}
\newcommand{\standard}[1]{\ensuremath{\mathrm{#1}^0}}

%% chimie
%% global
\make@globalVariable{Press}{P}
\make@globalVariable{Temp}{T}
\make@globalVariable{Vol}{V}
\make@globalVariable{nmol}{n}
\make@globalConstant{Pext}{P_{ext}}
\make@equation     {Rgp}  {\mathrm{R}}            {8.3144621} {75}{J\,mol^{-1}\,K^{-1}}
\make@equation[-23]{kBolt}{\mathrm{k}_\text{B}}   {1.3806488} {13}{J\,K^{-1}}
\make@equation[23] {NAvo} {\mathcal{N}_\text{Avo}}{6.02214129}{27}{mol^{-1}}
%%
\newcommand{\StateVarEtatGP}[2][\relax]{%
\ifx#1\relax%
  \def\@opt{}%
\else%
  \def\@opt{{_{#1}}}%
\fi%
\ensuremath{%
\ifx#2P%
  \frac{\nmol\@opt\Rgp\Temp\@opt}{\Vol\@opt}%
\else
  \ifx#2\Temp%
    \frac{\Press\@opt\Vol\@opt}{\nmol\@opt\Rgp}
  \else
    \ifx#2\Vol
      \frac{\nmol\@opt\Rgp\Temp\@opt}{\Press\@opt}
    \else
      \ifx#2\nmol%
        \frac{\Press\@opt\Vol\@opt}{\Rgp\Temp\@opt}
    \fi
  \fi
\fi%
}}

\newcommand{\Mm}[1]{\ensuremath{\mm_\ce{#1}}}
\newcommand{\ac}[1]{\ensuremath{a_{\ce{#1}}}}
\newcommand{\conc}[1]{\ensuremath{\left[\ce{#1}\right]}}

%%cinetique
\make@globalConstant{Keq}{K}
\make@globalConstant{@p}{p}
\newcommand{\pK}{\@p\Keq}
\newcommand{\pH}{\@p\ce{H}}
\newcommand{\M}{mol\,l$^{-1}$}
\make@globalConstant{mm}{M}

\newcommand{\pK@}[2]{\optional@chem[#1]{\pK\text{#2}}}
\newcommand{\pKs}[1][\relax]{\pK@{#1}{s}}
\newcommand{\pKa}[1][\relax]{\pK@{#1}{a}}
\newcommand{\pKd}[1][\relax]{\pK@{#1}{d}}

\newcommand{\K@}[2][\relax]{\optional@chem[#1]{\Keq_\text{#2}}}
\newcommand{\Ks}[1][\relax]{\K@[#1]{s}}
\newcommand{\Kd}[1][\relax]{\K@[#1]{d}}
\newcommand{\Ka}[1][\relax]{\K@[#1]{a}}
\newcommand{\Ke}[1][\relax]{\K@[#1]{e}}

\make@globalConstant{kcin}{k}

%%electrochimie
\make@globalVariable{potNernst}{E}
\make@globalConstant{F}{F}
\newcommand{\E}[1]{\optional@chem[#1]{\potNernst}}
\newcommand{\Ez}[1][\relax]{\optional@chem[#1]{\standard{\potNernst}}}
\newcommand{\Ezero}[1]{\Ez[#1]}

%%thermo
\make@globalVariable{chalSpec}{C}
\newcommand{\C}[1][\relax]{\optional@sub[#1]{\chalSpec}}
\make@globalVariable{Cv}   {\C[\Vol]}
\make@globalVariable{Cp}   {\C[\Press]}
\make@globalVariable{Gib}  {G}
\make@globalVariable{enth} {H}
\make@globalVariable{enIn} {U}
\make@globalVariable{entr} {S}
\make@globalVariable{chal} {Q}
\make@globalVariable{trav} {W}

\newcommand{\smallDelta}[3][\relax]{\optional@sub[#1] {\ensuremath{#2#3}}}
\newcommand{\bigDelta}  [2][\relax]{\optional@chem[#1]{\ensuremath{\Delta#2}}}
%%
\newcommand{\chaleur}   [1][\relax]{\optional@sub[#1] {\chal}}
\newcommand{\travail}   [1][\relax]{\optional@sub[#1] {\trav}}
\newcommand{\energie}   [1][\relax]{\optional@chem[#1]{\enIn}}
\newcommand{\Gibbs}     [1][\relax]{\optional@chem[#1]{\Gib}}
\newcommand{\enthalpie} [1][\relax]{\optional@chem[#1]{\enth}}
\newcommand{\entropie}  [1][\relax]{\optional@chem[#1]{\entr}}
\newcommand{\dchaleur}  [1][\relax]{\smallDelta[#1]{\delta}{\chal}}
\newcommand{\dtravail}  [1][\relax]{\smallDelta[#1]{\delta}{\trav}}
\newcommand{\denergie}  [1][\relax]{\smallDelta[#1]{\dd}{\enIn}}
\newcommand{\denthalpie}[1][\relax]{\smallDelta[#1]{\dd}{\enth}}
\newcommand{\dGibbs}    [1][\relax]{\smallDelta[#1]{\dd}{\Gib}}
\newcommand{\dentropie} [1][\relax]{\smallDelta[#1]{\dd}{\entr}}
\newcommand{\Denergie}  [1][\relax]{\bigDelta[#1]{\enIn}}
\newcommand{\Denthalpie}[1][\relax]{\bigDelta[#1]{\enth}}
\newcommand{\DGibbs}    [1][\relax]{\bigDelta[#1]{\Gib}}
\newcommand{\Dentropie} [1][\relax]{\bigDelta[#1]{\entr}}
\newcommand{\DGz}       [1][\relax]{\optional@chem[#1]{\standard{\DGibbs}}}
\newcommand{\DGzero}    [1]{\DGz[#1]}
\newcommand{\DrG}       [1][\relax]{\optional@chem[#1]{\ensuremath{\Delta_r\Gibbs}}}
\newcommand{\DrGz}      [1][\relax]{\optional@chem[#1]{\ensuremath{\Delta_r\text{\Gibbs}^0}}}
\newcommand{\formation} [2][\relax]{\optional@chem[#1]{#2_f}}
\newcommand{\Dformation}[2][\relax]{\bigDelta{\formation[#1]{#2}}}

%% chemistry equation 
\newcounter{chemeq}
\renewcommand{\thechemeq}{R-\arabic{chemeq}}
\newenvironment{chemicalEquation}
{\begin{displaymath}}
{\refstepcounter{chemeq}\tag{\thechemeq}%
\end{displaymath}}
\@addtoreset{chemeq}{exo}
\newcommand{\displayChem}[1]
{\begin{displaymath}
\ce{#1}
\end{displaymath}}

%% dessins
\tikzset{
atom/.style={circle,ball color=#1,outer sep=0.5pt},
N/.style={atom=green!80!black,inner sep=4pt},
C/.style={atom=black,inner sep=4pt},
H/.style={atom=white,inner sep=2pt},
O/.style={atom=red,inner sep=4pt}
}

\AtBeginDocument{%
\setcounter{chemeq}{0}
}
\endinput
